package cn.JHai.rbac.service.imp;

import cn.JHai.rbac.comm.CurrentUtil;
import cn.JHai.rbac.domain.Customer;
import cn.JHai.rbac.domain.Employee;
import cn.JHai.rbac.mapper.CustomerMapper;
import cn.JHai.rbac.query.*;
import cn.JHai.rbac.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CustomerImp implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public int Delete(Long id) {
        return customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Customer get(Long id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Customer> findAll() {
        return customerMapper.selectAll();
    }

    @Override
    public int saveOrUpdata(Customer customer) {
        //是插入
        System.out.println(customer.getId());
        if(customer.getId()==null){
            Employee current = CurrentUtil.getCurrent();
            customer.setStatus(0l);
            customer.setInput_time(new Date());
            customer.setInput_user_id(current.getId());
            customer.setSeller_id(current.getId());
            return customerMapper.insert(customer);

        }else {
            //跟新
            Customer customer2 = customerMapper.selectByPrimaryKey(Long.valueOf(customer.getId()));
            //反射遍历该对象的所有属性
            /*System.out.println("---"+customer2+"---");
            if (customer.getInput_time()==null){

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");//设置时间格式
                try {
                    Date ts = sdf.parse(customer2.getInput_time());//转换为Date类型 这里要抛一个异常
                    customer.setInput_time(ts);
                } catch (ParseException e) {
                    e.printStackTrace();

                }
            }
            Field[] fields = customer.getClass().getDeclaredFields();
            System.out.println(fields.length);
            for(int j=0 ; j<fields.length ; j++) {     //遍历所有属性
                String name = fields[j].getName();    //获取属性的名字
                Object value=this.getFieldValueByName(name,customer);
                System.out.println(value);
                if(value==null){

                    Object fieldValueByName = this.getFieldValueByName(name, customer2);
                    this.setFieldValueByName(name,customer,fieldValueByName);
                }
            }
            System.out.println(customer);*/
            return customerMapper.updateByPrimaryKey(customer);
        }
    }

    @Override
    public PageResult query(QueryObject queryObject, QueryCustomer queryCustomer) {

        if("-1".equals(queryCustomer.getSellerId())){
            queryCustomer.setSellerId("");
        }
        if("-1".equals(queryCustomer.getStatus())){
            queryCustomer.setStatus("");
        }
        Long count = customerMapper.selectForCount(queryCustomer);
        //List<Customer> Customers =null;
        if(count<=3l){
            //Customers = CustomerMapper.selectAll();
        }
        else{
            //Customers = CustomerMapper.pageSelect(queryObject);
        }
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),customerMapper.pageSelect(queryObject,queryCustomer),count);

    }

    @Override
    public List<QueryNameAndId> selectSeller() {
        return customerMapper.selectSeller();
    }

    @Override
    public List<QueryTittleAndId> selectJobs() {
        return customerMapper.selectJobs();
    }

    @Override
    public List<QueryTittleAndId> selectsources() {
        return customerMapper.selectsources();
    }

    @Override
    public List<QueryTittleAndId> selectContacts() {
        return customerMapper.selectContacts();
    }

    public void ChangeSeller(Long id,Long eid){
        customerMapper.ChangeSeller(id,eid);

    }

    @Override
    public void ChangeStaus(Long id, Long stus) {
        customerMapper.ChangeStaus(id,stus);
    }

    private static Object setFieldValueByName(String filedName, Object o,Object args) {
        try {
            String firstLetter = filedName.substring(0,1).toUpperCase();
            String getter = "set"+firstLetter+filedName.substring(1);
            Method method =o.getClass().getMethod(getter,new Class[]{args.getClass()});
            method.invoke(o,new Object[] {args});
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static Object getFieldValueByName(String filedName, Object o) {
        try {
            String firstLetter = filedName.substring(0,1).toUpperCase();
            String getter = "get"+firstLetter+filedName.substring(1);
            Method method =o.getClass().getMethod(getter);
            Object value = method.invoke(o);
            return value;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
